package com.cls.business.controller;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.cls.common.annotation.ControllerEndpoint;
import com.cls.common.utils.MyUtil;
import com.cls.common.entity.MyConstant;
import com.cls.common.controller.BaseController;
import com.cls.common.entity.QueryRequest;
import com.cls.common.entity.ResponseVO;
import com.github.pagehelper.PageInfo;
import com.cls.business.entity.DelayApply;
import com.cls.business.service.IDelayApplyService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PathVariable;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 延期申请表 Controller
 *
 * @author slx
 * @date 2020-12-02 14:26:56
 */
@Slf4j
@Validated
@Controller
@RequiredArgsConstructor
public class DelayApplyController extends BaseController {

    private final IDelayApplyService delayApplyService;

    /**
    * 延期申请表列表页面
    **/
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/delayApply")
    public String delayApplyIndex(){
        return MyUtil.view("business/delayApply/delayApply");
    }

    /**
    * 延期申请表新增页面
    * @return
    */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/delayApply/delayApplyAdd")
    public String delayApplyAdd(Model model){
        return MyUtil.view("business/delayApply/delayApplyAdd");
    }

    /**
    * 延期申请表修改页面
    * @return
    */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/delayApply/delayApplyUpdate/{delayApplyId}")
    public String delayApplyUpdate(@PathVariable Long delayApplyId, Model model){
        DelayApply delayApply = delayApplyService.getById(delayApplyId);
        model.addAttribute("delayApply",delayApply);
        return MyUtil.view("business/delayApply/delayApplyUpdate");
    }



    /**
    * 延期申请表详情页面
    * @return
    */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/delayApply/delayApplyDetail/{delayApplyId}")
    public String delayApplyDetail(@PathVariable Long delayApplyId, Model model){
        DelayApply delayApply = delayApplyService.getById(delayApplyId);
        model.addAttribute("delayApply",delayApply);
        return MyUtil.view("business/delayApply/delayApplyDetail");
    }

    @GetMapping("delayApply")
    @ResponseBody
    @RequiresPermissions("delayApply:list")
    public ResponseVO getAllDelayApplys(DelayApply delayApply) {
        return ResponseVO.success(delayApplyService.findDelayApplys(delayApply));
    }

    @GetMapping("delayApply/list")
    @ResponseBody
   /* @RequiresPermissions("delayApply:list")*/
    public ResponseVO delayApplyList(QueryRequest request, DelayApply delayApply) {

        PageInfo<DelayApply> pageInfo =  this.delayApplyService.selectAll(request, delayApply);
        return ResponseVO.success(pageInfo);
    }

    @ControllerEndpoint(operation = "新增DelayApply", exceptionMessage = "新增DelayApply失败")
    @PostMapping("delayApply")
    @ResponseBody
    @RequiresPermissions("delayApply:add")
    public ResponseVO addDelayApply(@Valid DelayApply delayApply) {
        this.delayApplyService.createDelayApply(delayApply);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "多个删除DelayApply", exceptionMessage = "删除DelayApply失败")
    @PostMapping("delayApply/deletes/{ids}")
    @ResponseBody
    @RequiresPermissions("delayApply:delete")
    public ResponseVO deleteDelayApplys(@PathVariable String ids) {
        String[] idArry = ids.split(StringPool.COMMA);
        this.delayApplyService.deleteDelayApplys(idArry);
        return ResponseVO.success();
    }


    @ControllerEndpoint(operation = "删除单个DelayApply", exceptionMessage = "删除DelayApply失败")
    @PostMapping("delayApply/delete/{delayApplyId}")
    @ResponseBody
    @RequiresPermissions("delayApply:delete")
    public ResponseVO deleteDelayApply(@PathVariable Long delayApplyId) {
        this.delayApplyService.removeById(delayApplyId);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "修改DelayApply", exceptionMessage = "修改DelayApply失败")
    @PostMapping("delayApply/update")
    @ResponseBody
    @RequiresPermissions("delayApply:update")
    public ResponseVO updateDelayApply(DelayApply delayApply) {
        this.delayApplyService.updateDelayApply(delayApply);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "修改DelayApply", exceptionMessage = "导出Excel失败")
    @PostMapping("delayApply/excel")
    @ResponseBody
    @RequiresPermissions("delayApply:export")
    public void export(QueryRequest queryRequest, DelayApply delayApply, HttpServletResponse response) {
        List<DelayApply> delayApplys = this.delayApplyService.findDelayApplys(queryRequest, delayApply).getList();
        ExcelKit.$Export(DelayApply.class, response).downXlsx(delayApplys, false);
    }
}
